// Code from Visualizing Data, First Edition, Copyright 2008 Ben Fry.
// Based on the GraphLayout example by Sun Microsystems.


class Node {
	float x, y;
	float dx, dy;
	boolean fixed;
	Artist artist;
	int count;

	Node(Artist artist,boolean central) {
		this.artist = artist;
		if(!central){
		x = GLOBAL.processing.random(GLOBAL.width-250);
		y = GLOBAL.processing.random(GLOBAL.height-GLOBAL.infoPanel.y)+GLOBAL.infoPanel.y;
		}
		else{
			if(GLOBAL.graphsArea.networkGraph.numberOfCenters == 1){
				x = (GLOBAL.width-250)/2;
				y = (GLOBAL.height-GLOBAL.infoPanel.y)/2 + GLOBAL.infoPanel.y;
			}
			else if (GLOBAL.graphsArea.networkGraph.numberOfCenters  == 2){
				if(GLOBAL.graphsArea.networkGraph.numberOfCentersCreated == 0){
				x = (GLOBAL.width-250)/2- (GLOBAL.width-250)/6;
				y = (GLOBAL.height-GLOBAL.infoPanel.y-50)/2 + GLOBAL.infoPanel.y - (GLOBAL.height-GLOBAL.infoPanel.y-50)/5;
				}
				else {
					x = (GLOBAL.width-250)/2+ (GLOBAL.width-250)/6;
					y = (GLOBAL.height-GLOBAL.infoPanel.y-50)/2 + GLOBAL.infoPanel.y +(GLOBAL.height-GLOBAL.infoPanel.y-50)/5;
				}
				GLOBAL.graphsArea.networkGraph.numberOfCentersCreated++;

			}

		}
		//x = (GLOBAL.width-250)/2;
		//y = (GLOBAL.height-GLOBAL.infoPanel.y)/2 + GLOBAL.infoPanel.y;
	}


	void increment() {
		count++;
	}


	void relax() {
		//allontana
		float ddx = 0;
		float ddy = 0;
		for (int j = 0; j < GLOBAL.graphsArea.networkGraph.nodeCount; j++) {
			Node n = GLOBAL.graphsArea.networkGraph.nodes[j];
			if (n != this) {
				float vx = x - n.x;
				float vy = y - n.y;
				float lensq = vx * vx + vy * vy;
				if (lensq == 0) {
					ddx += GLOBAL.processing.random(1);
					ddy += GLOBAL.processing.random(1);
				} else if (lensq > 100) {
					ddx += vx / lensq;
					ddy += vy / lensq;
				}
			}
		}
		float dlen = GLOBAL.processing.mag(ddx, ddy) / 2;
		if (dlen > 0.017) {
			dx += ddx / dlen;
			dy += ddy / dlen;
		}

	}


	void update() {
		if (!fixed) {      
			x += GLOBAL.processing.constrain(dx, -5, 5);
			y += GLOBAL.processing.constrain(dy, -5, 5);

			x = GLOBAL.processing.constrain(x, 0+GLOBAL.graphsArea.networkGraph.xOffset, GLOBAL.width-250-GLOBAL.graphsArea.networkGraph.xOffset);
			y = GLOBAL.processing.constrain(y, GLOBAL.infoPanel.y+GLOBAL.graphsArea.networkGraph.yOffset, GLOBAL.height-GLOBAL.graphsArea.networkGraph.yOffset-70);
		}
		dx /= 2;
		dy /= 2;
	}


	void draw() {
		GLOBAL.processing.fill(GLOBAL.graphsArea.networkGraph.nodeColor);
		GLOBAL.processing.stroke(30);
		GLOBAL.processing.strokeWeight((float)0.5);
		GLOBAL.processing.textFont(GLOBAL.plotFont, 13);
		GLOBAL.processing.fill(GLOBAL.graphsArea.networkGraph.nodeBackColor);
		float w = GLOBAL.processing.textWidth(artist.arName);
		GLOBAL.processing.ellipse(x, y, w+20, 45);

		GLOBAL.processing.fill(GLOBAL.textColor);

		GLOBAL.processing.textAlign(GLOBAL.processing.CENTER, GLOBAL.processing.CENTER);
		GLOBAL.processing.text(artist.arName, x, y);
	}
}
